package com.framework.base.taglib.page; 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.framework.base.constant.WebConstants;

/**
 * Description:分页标签
 * tabaleName(对应的表名):
 * @author：www.51wuxia.com
 * @version 1.0
 * @Date Jun 9, 2011 5:25:11 PM
 */
public class PaginationTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	private final static String TABLE_TAG = "<table width=\"90%\" style=\"font-size:12px;\" border=\"0\" cellspacing=\"3\" cellpadding=\"2\">";
	private final static String TABLE_END_TAG = "</table>";
	private final static String TR_TAG = "<tr>";
	private final static String TR_END_TAG = "</tr>";
	private final static String TD_CENTER_TAG = "<td nowrap width=\"45%\" align=\"left\" >";
	private final static String TD_END_TAG = "</td>";
	private final static String TD_RIGHT_TAG = "<td nowrap width=\"55%\" align=\"right\" class=\"xz_txt\">";
	private final static String HIDDEND_CURRENTPAGE = "<input type=\"hidden\" name=\""+WebConstants.CURRENTPAGE+"\" value=";// 没完成
	private final static String HIDDEND_PAGINATION_ACTION = "<input type=\"hidden\" name=\"paginationAction\" value=\"\"><input type=\"hidden\" name=\"paramsUrl\" value=\"\">";
	protected boolean filter = true;// 是否过滤html
	private boolean order;
	private String orderFids=WebConstants.DEFAULTORDERFIDS; //排序字段
	private String orderType=WebConstants.DEFAULTORDERTYPE; //排序类型
	private String imagesPath="/common/images/";
	private Pagination pagination;
	private int formIndex=0;//为提交的是哪个表单 默认为0
	public PaginationTag() {
		order = true;
	}

	// 组装String
	@SuppressWarnings("static-access")
	private String poplutateString() throws java.io.IOException, JspException {
		String enter = "\r\n";
		String empty = "&nbsp";
		String context = ((HttpServletRequest) pageContext.getRequest())
				.getContextPath();
		// 定义变量----start
		// 应该从system.properties中读
		String IMAGE_NO_FIRST = "<img src=\""
				+ context
				+imagesPath+"/sye_03.gif\" alt=\"第一页\" width=\"39\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		String IMAGE_NO_NEXT = "<img src=\""
				+ context
				+imagesPath+"/fnye_05.gif\" alt=\"下一页\" width=\"27\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		String IMAGE_NO_PERVIO = "<img src=\""
				+ context
				+imagesPath+"/fnye_05_2.gif\" alt=\"上一页\" width=\"27\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		String IMAGE_NO_LAST = "<img src=\""
				+ context
				+imagesPath+"/sye_05.gif\" alt=\"最后一页\" width=\"39\" height=\"17\" border=\"0\">"; // efrom2应动态生成

		
		String IMAGE_FIRST = "<img src=\""
				+ context
				+imagesPath+"/sye_03.gif\" alt=\"第一页\" width=\"39\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		String IMAGE_NEXT = "<img src=\""
				+ context
				+imagesPath+"/fnye_05.gif\" alt=\"下一页\" width=\"27\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		String IMAGE_PERVIO = "<img src=\""
				+ context
				+imagesPath+"/fnye_05_2.gif\" alt=\"上一页\" width=\"27\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		String IMAGE_LAST = "<img src=\""
				+ context
				+imagesPath+"/sye_05.gif\" alt=\"最后一页\" width=\"39\" height=\"17\" border=\"0\">"; // efrom2应动态生成
		// 定义变量----end
		StringBuffer sb = new StringBuffer("");
		String submitHref = "<a href=\"javascript:document."
				+ "forms["+getFormIndex()+"]" + ".submit()\"";
		submitHref += " oncontextmenu=\"return false\"  onclick=\"document."
				+ "forms["+getFormIndex()+"]"
				+ ".paginationAction.value='"
				+ pagination.FIRST_ACTION + "';\">";
		sb.append(TABLE_TAG + enter);
		sb.append(TR_TAG + enter);
		sb.append(TD_CENTER_TAG + enter);
		if(pagination.getTotalPage()==0) {
			pagination.setCurrentPage(0);
		}
		sb.append(" 共<font color='#CC3300'>" + pagination.getTotalCount()
				+ "</font>记录" + "      当前第<font color='#CC3300'>"
				+ pagination.getCurrentPage() + "/" + String.valueOf(pagination.getTotalPage())
				+ "</font>页  每页<font color='#CC3300'>");
		sb.append(pagination.getRowsPerPage() + "</font>条" + enter);
		
		sb.append(TD_END_TAG + enter + TR_END_TAG + enter + TABLE_END_TAG);
		sb.append(TD_END_TAG + enter);
		sb.append(TD_RIGHT_TAG + enter);
		//第二行
		if (pagination.firstEnable())
			sb.append(submitHref + IMAGE_FIRST + "</a>" + empty + enter);
		else
			sb.append(IMAGE_NO_FIRST + empty + enter);
		if (pagination.previoEnable()) {
			String pervioHref = submitHref.replaceFirst(
					".paginationAction.value='" + pagination.FIRST_ACTION
							+ "';", ".paginationAction.value='"
							+ pagination.PREVIO_ACTION + "';");
			sb.append(pervioHref + IMAGE_PERVIO + "</a>" + empty + enter);
		} else
			sb.append(IMAGE_NO_PERVIO + empty + enter);
		if (pagination.nextEnable()) {
			String nextHref = submitHref.replaceFirst(
					".paginationAction.value='" + pagination.FIRST_ACTION
							+ "';", ".paginationAction.value='"
							+ pagination.NEXT_ACTION + "';");
			sb.append(nextHref + IMAGE_NEXT + "</a>" + empty + enter);
		} else
			sb.append(IMAGE_NO_NEXT + empty + enter);
		if (pagination.lastEnable()) {
			String lastHref = submitHref.replaceFirst(
					".paginationAction.value='" + pagination.FIRST_ACTION
							+ "';", ".paginationAction.value='"
							+ pagination.LAST_ACTION + "';");
			sb.append(lastHref + IMAGE_LAST + "</a>" + empty + enter);
		} else
			sb.append(IMAGE_NO_LAST + empty + enter);
		String myForm = "document." + "forms["+getFormIndex()+"]";
		sb.append("<a href=\"javascript:" + myForm + ".submit()\"");
		sb.append(" oncontextmenu=\"return false\" onclick=\"if(" + myForm
				+ ".pageSelect.value==''){ alert('页码必须输入');return false;}"
				+ enter);
		sb.append(" else {" + myForm
				+ ".paginationAction.value='gotoPage';}\">跳到</a>第" + enter);
		sb
				.append("<input type=\"hidden\" name=\""+WebConstants.ORDERFIDS+"\" value=\""+this.getOrderFids()+"\"><input type=\"hidden\" name=\""+WebConstants.ORDERTYPE+"\" value=\""+this.getOrderType()+"\"><input type=text size='4' onlytype='int' onfocus='checkTextBoxInput()' name='pageSelect' value='' style='background:url("+context+imagesPath+"/fnye_10.gif) no-repeat; width:27px; height:17px;'/>页"
						+ enter);
		sb.append(HIDDEND_CURRENTPAGE + "\"" + pagination.getCurrentPage()
				+ "\">" + enter);
		sb.append(HIDDEND_PAGINATION_ACTION + enter);

		return new String(sb);
	}

	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.print(this.poplutateString());
		} catch (java.io.IOException ie) {
			ie.printStackTrace();
			throw new JspException("错误信息: 在处理翻页时出错");
		}
		return 0;
	}

	public void release() {
		super.release();
		filter = true;
	}

	// setter/getter
	public boolean isFilter() {
		return filter;
	}

	public void setFilter(boolean filter) {
		this.filter = filter;
	}


	public boolean isOrder() {
		return order;
	}

	public void setOrder(boolean order) {
		this.order = order;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public int getFormIndex() {
		return formIndex;
	}

	public void setFormIndex(int formIndex) {
		this.formIndex = formIndex;
	}

	public String getOrderFids() {
		return orderFids;
	}

	public void setOrderFids(String orderFids) {
		this.orderFids = orderFids;
	}

	public String getOrderType() {
		return orderType;
	}

	public void setOrderType(String orderType) {
		this.orderType = orderType;
	}

	
}
